package com.mdd.front.controller;

import com.mdd.common.aop.NotLogin;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

/**
 * @Author: zy
 * @Date: 2025-03-08-12:43
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping("api/aaa")
@Api(tags = "签到")
public class SignController {

    @Autowired
    private RedisTemplate redisTemplate;

    @NotLogin
    @GetMapping("/sign")
    public Boolean sign() {
        try {
            //1. 获取登录用户
            Long userId = 1001012345L;
            //2. 获取日期
            for (int i = 0; i < 10; i++) {
                LocalDate date = LocalDateTime.now().minusMonths(2).minusDays(i).toLocalDate();
                String format = date.format(DateTimeFormatter.ofPattern(":yyyyMM"));

                //3. 拼接key
                String keySuffix = format;
                String key = "sign:" + userId + keySuffix;
                log.info("redis key:" + key);
                //4. 获取今天是本月的第几天
                int dayOfMonth = date.getDayOfMonth();
                //5. 写入redis setbit key offset 1
                System.out.println("dayOfMonth:" + dayOfMonth);
                Boolean aBoolean = redisTemplate.opsForValue().setBit(key, dayOfMonth - 1, true);
                System.out.println("boolean:" + aBoolean);

            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return true;
    }

    @NotLogin
    @GetMapping("/signCount")
    public String signCount() {

        long l = countSignInDays("sign:1001012345:202505");
        System.out.println("l:" + l);
        return String.valueOf(l);
    }

    /**
     * 统计当前月的签到总数
     *
     * @param key Redis键
     * @return 签到总数
     */
    public long countSignInDays(String key) {
        // 获取当前月的总天数
        YearMonth currentYearMonth = YearMonth.now();
        int daysInMonth = currentYearMonth.lengthOfMonth();

        // 使用 execute 方法执行 BITCOUNT 命令
        String finalKey = key;
        Long bitCount = (Long) redisTemplate.execute((RedisConnection connection) -> {
            // 检查键是否存在
            if (!connection.exists(finalKey.getBytes())) {
                return 0L;
            }
            // 执行 BITCOUNT 命令，统计指定范围内的 1 的数量
            return connection.bitCount(finalKey.getBytes(), 0, daysInMonth - 1);
        });

        return Optional.ofNullable(bitCount).orElse(0L);
    }

    public static void main(String[] args) {
        // 获取当前月的总天数
        YearMonth currentYearMonth = YearMonth.now();
        int daysInMonth = currentYearMonth.lengthOfMonth();
        System.out.println(daysInMonth);
    }

}
